﻿<?php

//验证签名的文件
include_once './func/common.php';
include_once './func/secureUtil.php';

include "/var/www/html/new/conf/le.je";
$le = new go_sql();
$le->connect();

require_once ("/var/www/html/new/d/alipay/ali_order/func.inc.php");//total_fee fenRun write_logs
define('LOG_NAME', '/var/www/html/new/d/logs/union_pay/logs/'.date('ymd').'.log');//log文件路径
$sourceT = 2;//银联的

write_logs('开始验证:
'.json_encode($_POST), LOG_NAME);

if (isset ( $_POST ['signature'] )) {
    if(!verify ( $_POST )){
        write_logs('验签失败', LOG_NAME);
        exit('chkFail');
    }
    write_logs('验签成功', LOG_NAME);
    if($_POST['respMsg']!='Success!') {
        write_logs('返回消息不成功', LOG_NAME);
        exit;
    }
    //订单号  因为长度不足 前面补上了PFT三个字母
    $ordern = str_replace('PFT','',$_POST['orderId']);
    $str="select status,total_fee from pft_alipay_rec where out_trade_no='$ordern' AND `sourceT`=$sourceT LIMIT 1";
    $GLOBALS['le']->query($str);
    $pay_log_data = $GLOBALS['le']->fetch_assoc();
    if (!$pay_log_data) {
        write_logs('支付log不存在'.$str, LOG_NAME);
        $GLOBALS['le']->close();
        exit('no data');
    }
    $status = $GLOBALS['le']->f('status');
    if($status==1){
        write_logs('状态已经完成', LOG_NAME);
        $GLOBALS['le']->close();
        exit('fail');
    }
    $pay_total_fee = (int)$_POST['txnAmt'];                //回传的信息里单位是分
    $total_fee = (string)($GLOBALS['le']->f("total_fee") * 100);//数据库中的单位是元
    if ($pay_total_fee != $total_fee) {
        write_logs('支付金额不等。返回金额:' . $pay_total_fee . ',实际金额:'. $total_fee, LOG_NAME);
        $GLOBALS['le']->close();
        exit('fail');
    }
    $soap = new SoapClient(null,array(
            "location" => "http://localhost/open/openService/pft_insideMX.php",
            "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
    );
    require("/var/www/html/new/d/class/OrderRecord.php");//查询订单
    $or = new OrderRecord($ordern, $soap);
    $orders=$or->info();

    if((string)$orders['mainOrder']->UUpaystatus==1) {
        write_logs('状态已经完成', LOG_NAME);
    }

    $aid = (int)$orders['mainOrder']->UUaid;
    $mid = (int)$orders['mainOrder']->UUmid;

    $trade_no = $_POST['queryId'];//银联的交易流水号
    //$buyer_email = $_POST['accNo'];// 没有这个字段
    $str=<<<SQL
UPDATE pft_alipay_rec set status=1,dtime={$_POST['txnTime']},`trade_no`='$trade_no'
WHERE out_trade_no='$ordern' AND `sourceT`=$sourceT limit 1
SQL;
    $GLOBALS['le']->query($str);
    //判断产品供应商是不是刘三姐票务中心
//    $memo = $aid==43517?'广西票付通':'';
    $alipay_to_pft = true;
    $total_fee = total_fee($orders);// 获取总价格
    $p_type = 5;//银联支付
    if($aid!=$mid) {
        // 非自身供应须分润  返回值是上级的利润
        $aid_money = fenRun($soap, $ordern, $aid, $mid, $total_fee, $orders, $alipay_to_pft,$p_type);
    } else{// 支付给票付通需给自供自销的分销商加上相应金额
        if($alipay_to_pft){
            $soap->PFT_Member_Fund_Modify($mid, 0, $total_fee, 0, 0,null,0, 5,$ordern);
        }
    }
    //更新远端订单号,银联支付
    $soap->Remote_Ordernum_Update(serialize(array($ordern=>'PC_UNI_PFT_'.$_SERVER['REQUEST_TIME'])));

    /*只更新订单支付状态*/
    $soap->Mod_Order_Status($ordern,1,7);//1表示paystatus改成1 已支付   7表示 pmode改成7 银联支付

//    //演出订单更新座位状态
//    if((string)$orders['mainOrder']->UUp_type=='H')
//    {
//        $relation_info_req = http_build_query(array(
//            'Action'  => 'Relation_after_pay',
//            'Ordern'  => $ordern,
//            'Fid'     => (int)$orders['mainOrder']->UUmid,
//        ));
//        file_get_contents('http://10.160.4.140/new/d/api/Product_check_h.php?'.$relation_info_req);
//    }
    // 支付后交互
    $tid = (int)$orders['mainOrder']->UUtid;
    $sql = "select Mpath,Mdetails from uu_jq_ticket where id=$tid limit 1";
    $GLOBALS['le']->query($sql);
    $GLOBALS['le']->fetch_assoc();
    if(($Mpath=$GLOBALS['le']->f('Mpath'))){

        $relation_info_req = http_build_query(array(
            'Action'  => 'Relation_after_pay',
            'Ordern'  => $ordern,
            'Fid'     => (int)$orders['mainOrder']->UUmid,
        ));
        file_get_contents($Mpath.'?'.$relation_info_req);
    }
    //最后重发短信
    include '/var/www/html/new/d/class/OrderSmsManual.class.php';
    $smsManual  = new \PFT\SMS\OrderSmsManual($soap);
    $ret = $smsManual->SendByOrderInfo($orders,true,false);//通知供应商 不发送微信通知

    write_logs('后台处理完毕', LOG_NAME);
    $GLOBALS['le']->close();
} else {
    write_logs('签名为空', LOG_NAME);
    exit('chkLost');
}



?>

